package com.xiaoxie.cloud.controller;

import com.xiaoxie.cloud.service.FlowLimitService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class FlowLimitController {
    @GetMapping("/testA")
    public String testA() {
        return "-----testA";
    }

    @GetMapping("/testB")
    public String testB() {
        return "-----testB";
    }

    @Resource
    private FlowLimitService flowLimitService;

    @GetMapping("/testC")
    public String testC() {
        flowLimitService.common();
        return "-----testC";
    }

    @GetMapping("/testD")
    public String testD() {
        flowLimitService.common();
        return "-----testD";
    }


    @GetMapping("/testE")
    public String testE() {
        log.info("-------testE排队等待--------");
        return "-----testE";
    }


    /**
     * 熔断规则：慢调用比例
     * @return
     */
    @GetMapping("/testF")
    public String testF() {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("-------testF:新增熔断规则-慢调用比例--------");
        return "-----testF:新增熔断规则-慢调用比例";
    }

    /**
     * 熔断规则：异常比例
     * @return
     */
    @GetMapping("/testG")
    public String testG() {
        log.info("-------测试：新增熔断规则-异常比例");
        int age = 10 / 0; // 这里会抛出异常
        return "-----testG:新增熔断规则-异常比例";
    }

    /**
     * 熔断规则：异常数
     * @return
     */
    @GetMapping("/testH")
    public String testH() {
        log.info("-------testH:新增熔断规则-异常数");
        int age = 10 / 0; // 这里会抛出异常
        return "-----testH:新增熔断规则-异常数";
    }

}
